package com.yami.shop.distribution.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.common.dto.AchievementDataDto;
import com.yami.shop.distribution.common.dto.DistributionOrderDto;
import com.yami.shop.distribution.common.dto.DistributionUserDto;
import com.yami.shop.distribution.common.dto.DistributionUserSimpleDto;
import com.yami.shop.distribution.common.model.DistributionMsg;
import com.yami.shop.distribution.common.model.DistributionUser;
import com.yami.shop.distribution.common.service.DistributionUserIncomeService;
import com.yami.shop.distribution.common.service.DistributionUserService;
import com.yami.shop.distribution.common.vo.DistributionRecruitConfigVO;
import com.yami.shop.distribution.common.vo.DistributionUserInfoVO;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.yami.shop.common.util.BeanUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author yami
 */
@RestController
@RequestMapping("/p/distribution/user")
@Tag(name = "分销员接口")
@AllArgsConstructor
public class DistributionUserController {

    private final DistributionUserService distributionUserService;

    private final DistributionUserIncomeService distributionUserIncomeService;

    private final SysConfigService sysConfigService;



    @GetMapping("/info")
    @Operation(summary =  "查看分销中心信息")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<AchievementDataDto> info(@RequestParam(name="shopId", required = false, defaultValue = "0") Long shopId) {
//        Long shopId = 0L;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        if (Objects.isNull(distributionUser)) {
            // 您还不是销售员
            throw new YamiShopBindException("yami.distribution.user.no.distributor");
        }
        return ServerResponseEntity.success(distributionUserService.getAchievementDataDtoById(distributionUser.getDistributionUserId()));
    }

    @GetMapping("/distributionUserOrders")
    @Operation(summary =  "分页查看分销员佣金明细", description = "传入分销员id")
    public ServerResponseEntity<IPage<DistributionOrderDto>> getDistributionOrderDtosByDistributionUserId(PageParam<DistributionOrderDto> page) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        return ServerResponseEntity.success(distributionUserIncomeService.getDistributionOrderDtoByDistributionUserId(page, distributionUser.getDistributionUserId()));
    }

    @GetMapping("/distributionUserInfo")
    @Operation(summary =  "分销员信息")
    public ServerResponseEntity<DistributionUserDto> isDistributionUser() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        // 推广开关
        DistributionRecruitConfigVO distributionConfigVO = sysConfigService.getSysConfigObject(Constant.DISTRIBUTION_RECRUIT_CONFIG, DistributionRecruitConfigVO.class);
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        DistributionUserDto distributionUserDto = BeanUtil.map(distributionUser, DistributionUserDto.class);
        if (distributionUserDto == null) {
            distributionUserDto = new DistributionUserDto();
            distributionUserDto.setRecruitState(distributionConfigVO.getState());
            return ServerResponseEntity.success(distributionUserDto);
        }
        distributionUserDto.setRecruitState(distributionConfigVO.getState());
        return ServerResponseEntity.success(distributionUserDto);
    }

    @GetMapping("/subordinate")
    @Operation(summary =  "查看下级")
    public ServerResponseEntity<IPage<DistributionUserSimpleDto>> subordinatePage(PageParam<DistributionMsg> page) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        return ServerResponseEntity.success(distributionUserService.getDistributionUserSimpleDtoByParentUserIdPage(page, distributionUser.getDistributionUserId()));
    }

    @GetMapping("/subordinateUser")
    @Operation(summary =  "获取好友接口")
    @Parameters({
            @Parameter(name = "userType", description = "类型 1 我的用户， 2 我的好友")
    })
    public ServerResponseEntity<List<DistributionUserInfoVO>> subordinateUserPage(@RequestParam(name="userType") Integer userType){
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        List<DistributionUserInfoVO> myFriends = distributionUserService.getDistributionMyFriend(distributionUser, userType);
        return ServerResponseEntity.success(myFriends);
    }
}
